package 前端数据访问模拟;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.sql.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SQLHandle<T> {


    // 数据库连接
    private static final String URL = "jdbc:mysql://rdsap8226j2iuruyoaamgpublic.mysql.rds.aliyuncs.com/bbcare_oms";
    private static final String NAME = "gao_xiaolei";
    private static final String PASS = "gxl_111666";
    private static final String DRIVER = "com.mysql.jdbc.Driver";
    private static final String database = "bbcare_oms";
    private static Connection conn;

    static {
        try {
            Class.forName(DRIVER);
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    //获取数据库连接
    public static Connection getConn() {
        try {
            if (conn == null || conn.isClosed()) {
                conn = DriverManager.getConnection(URL, NAME, PASS);
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return conn;
    }

    //关闭资源
    public static void closeAll(ResultSet rs, Statement ps, Connection conn) {
        try {
            if (rs != null) {
                rs.close();
            }
            if (ps != null) {
                ps.close();
            }
            if (conn != null) {
                conn.close();
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


    public PreparedStatement getPS(PreparedStatement ps, Object... objects) {
        for (int i = 0; i < objects.length; i++) {
            try {
                ps.setObject(i + 1, objects[i]);
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return ps;
    }


    public Map<String, Object> setMap(Object obj, ResultSet rs) {
        HashMap<String, Object> hm = new HashMap<>();
        Field[] fields = obj.getClass().getDeclaredFields();
        try {
            if (rs.next()) {
                for (int i = 0; i < fields.length; i++) {
                    hm.put(fields[i].getName(), rs.getObject(fields[i].getName()));
                }
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return hm;

    }


    public T setT(T obj, Map<String, Object> map1) {

        Class c = obj.getClass();
        Method[] methods = c.getMethods();
        for (Method m : methods) {
            if (m.getName().startsWith("set")) {
                String name = m.getName();
                name = name.substring(3, 4).toLowerCase() + name.substring(4, name.length());

                if (map1.containsKey(name)) {
                    try {
                        m.invoke(obj, map1.get(name).toString());
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        }

        return obj;
    }

    public T GetData(String sql, T t) {


        ResultSet rs = null;
        Statement stm = null;
        try {
            stm = getConn().createStatement();
            rs = stm.executeQuery(sql);
            return setT(t, setMap(t, rs));

        } catch (Exception ex) {

        } finally {
            closeAll(rs, stm, conn);

        }
        return null;
    }


}
